

#include "OgreApplication.h"
#include "CEGUI.h"

HintTextContainer::HintTextContainer(int time,Ogre::String text){

	this->time = time;
	this->text = text;

}

HintTextContainer::~HintTextContainer(){
}

void HintTextContainer::update(unsigned long millis){
	this->time -= millis;
}

bool HintTextContainer::deleteThis(){
	return (this->time <= 0);
}




HintWindow::HintWindow(int hint_delay){
	
	this->layoutName	= "HintBox.layout";
	

	this->hintdel		= hint_delay;
	this->text_vector	= NULL;

	this->updatecounter = 0;

	this->initVector();

	this->load();
}

void HintWindow::initVector(){
	this->text_vector = new std::vector<HintTextContainer*>();
}

HintWindow::~HintWindow(){
	this->destroyVector();
}

void HintWindow::destroyVector(){
	if(this->text_vector == NULL)return;
	
	for(unsigned int i = 0; i < this->text_vector->size();i++){
		delete this->text_vector->at(i);
	}
	
	this->text_vector->clear();
	delete this->text_vector;
	this->text_vector = NULL;
}

CEGUI::Editbox*	HintWindow::getEditbox(){
if(this->cegui_window == NULL)return NULL;	
		CEGUI::Editbox* box =(CEGUI::Editbox*) this->cegui_window->getChild("HintBox/");
		return box;
}

void HintWindow::addText(Ogre::String text){
	if(this->text_vector == NULL)return;

	this->text_vector->push_back(new HintTextContainer(this->hintdel,text));

}

void HintWindow::update(unsigned long millis){
	if(this->updatecounter < 15){
	this->updatecounter++;
	
	return;
	}
	
	if(! this->shown){
		if(this->text_vector->size() == 0){
			this->updatecounter = 0;
			return;
		}
		else{
			this->show();
			this->updatecounter = 0;
		}
	}
	

	if(this->text_vector->size() == 0){
	this->hide();
	return;
	}

	int size = this->text_vector->size();

	for(int i = 0; i < size;i++){
		HintTextContainer* current = this->text_vector->at(i);
		
		current->update(millis*15);

		if(current->deleteThis()){
			delete current;this->text_vector->erase(this->text_vector->begin()+i);
			i--;size--;
		}
	}

	if(size == 0)return;
		
	Ogre::String sum = "";

	for(int i = 0; i < size;i++){
		sum = sum + "\n"+this->text_vector->at(i)->text;
	}

	this->getEditbox()->setText(sum);
	this->updatecounter = 0;
}